import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class stduentSelect {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("666666");

        Connection connection=dataSource.getConnection();
        System.out.println(connection);

        Scanner scanner=new Scanner(System.in);
        System.out.println("输入要查询的学生学号");
        int id=scanner.nextInt();
        System.out.println("输入要查询的学生名字");
        String name=scanner.next();
        String sql="select*from student where id=? or name=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setInt(1,id);
        ps.setString(2,name);
        ResultSet resultSet=ps.executeQuery();

        while(resultSet.next()){
            int id1=resultSet.getInt("id");
            String name1=resultSet.getString("name");
            System.out.println("id "+id1+" name"+name1);
        }


        resultSet.close();
        ps.close();
        connection.close();
    }
}
